#! /usr/bin/env perl
#   Copyright 2013 Stacy Doss stacy.doss@gmail.com
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

use warnings;
use strict;

use DIME qw(new);
use Getopt::Long;
use Storable qw(retrieve);

my %opt = (
		   array     => 0,
		   hash      => 0,
		   help      => 0,
		   usage     => 0,
		   manual    => 0,
		   ddd_file  => '',
		   psr_file  => '',
		   );

GetOptions("ddd=s"  => \$opt{ddd_file},
		   "psr=s"  => \$opt{psr_file},
		   "array"  => \$opt{array},
		   "hash"   => \$opt{hash},
		   "help"   => \$opt{help},
		   "usage"  => \$opt{usage},
		   "manual" => \$opt{manual});

#u_help   if $opt{help};
#u_usage  if $opt{usage};
#u_manual if $opt{manual};

my $f = $opt{psr_file} || $opt{ddd_file} || shift || '';
my $r;

if (-e $f) {
	if ($opt{psr_file}) {
		$r = retrieve $f;
	} elsif ($opt{ddd_file}) {
		$r = do $f;
	} elsif ($f =~ /\.psr$/) {
		#u_log "Assuming psr model from extention";
		$r = retrieve $f;
	} elsif ($f =~ /\.ddd$/) {
		#u_log "Assuming ddd model from extention";
		$r = do $f;
	} elsif (-B $f) {
		#u_log "Assuming psr model from file test";
		$r = retrieve $f;
	} elsif (-T $f) {
		#u_log "Assuming ddd model from file test";
		$r = do $f;
	}
}

unless (defined $r) {
	if ($opt{array} and not $opt{hash}) {
		#u_log "New structure with root of ARRAY",
		$r = [];
	}

	if ($opt{hash}) {
		#u_log "New structure with root of HASH",
		$r = {};
	}
}

unless (defined $r) {
	#u_log "Assuming you want a new structure of HASH";
	$r = {};
}

my $data = {foo => 'bar',
              fruit => ['apple', undef, 'bananna', 'orange', {jan => 'Jan', feb => 'Feb'}],
              grok => {
                test   => undef,
                rah    => 'Robert A. Heinlein',
                miahm  => 'Wyoming Knot',
                siasl  => ' Lazarus Long',
                tgif   => ['q', 'w', 'e', 'r', 't', 'y']} };

my $DIME = new DIME($data);

$DIME->show();

1;
__END__

=head1 NAME

dime.pl - Edit/Browse/Create .psr or .ddd files.

=head1 SYNOPSIS

dime.pl [-ddd file[.ddd]] [-psr file[.psr]] [-array | -hash] [file[.psr|.ddd]] 
[-help | -usage | -manual]

=head1 OPTIONS

=over 4

=item [file[.psr|.ddd]] 

Data structure file. If extention is .psr or .ddd dime.pl will force a read of that type, otherwise it will do a file test and open a text file as a ddd and a binary file as a psr.

=item -ddd file[.ddd] 

Specify filename and force ddd model

=item -psr file[.psr] 

Specify filename and force psr model

=item -array 

New data structure with array root.

=item -hash 

New data structure with hash root. This is also the default behavior if no file specification is given.

=item -help | -usage | -manual

Display help, usage or man page

=back

=head1 DESCRIPTION

Allows the user to browse, edit and create .psr & .ddd files (perl data structures)

=head1 EXAMPLE

=head1 RATIONAL

Needed tool to browse/edit complex perl data structures. 
Thus was born psr_ed.pl later shortened to just psr.pl 
and now dime.pl Functionality moved to DIME.pm for 
plug-and-play use. ddd (Data Dumper Dump) format added 
for additional flexability.

=head1 REQUIRES

=head1 HTML

=head1 SEE ALSO

DIME.pm

=head1 AREA

CAD - Tool

=head1 TODO

A LOT!

=head1 BUGS

Are you kidding?

=head1 AUTHOR

Stacy Doss - sdoss@hifn.com

=cut
